import string

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
from bs4 import BeautifulSoup
import xlwt
import xlrd
import math
import random
import os
from openpyxl import workbook
from xlutils.copy import copy


# 判断字符串是否包含中文
def is_chinese(check_str):
    for ch in check_str:
        if u'\u4e00' <= ch <= u'\u9fff':
            return True
    return False


def get_rows():
    try:
        # excel路径
        excle_path = r'D:/HInfos2.xls'
        # 打开excel读取文件
        data = xlrd.open_workbook(excle_path)
        # 根据sheet下标选择读取内容
        sheet = data.sheet_by_index(0)
        # 获取到表的总行数
        nrows = sheet.nrows - 1
    except FileNotFoundError:
        nrows = 0
    return nrows


def joshu(x):
    if x % 2 == 0:
        return 0
    else:
        return 1


class QunarSpider():
    # chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenum\AutomationProfile"
    def theSpider(self, city, startdate, enddate):
        chrome_options = Options()
        chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9221")
        driver = webdriver.Chrome(options=chrome_options)
        print(driver.title)
        homeUrl = 'https://hotel.qunar.com';
        driver.get(homeUrl)
        # 选取城市
        toCity = driver.find_element_by_xpath("//input[@tabindex='1']")
        toCity.clear()
        toCity.send_keys(city)
        element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "hot_key_list")))
        toCity.send_keys(Keys.TAB)
        time.sleep(2)
        # 选取结束日期
        checkOutDate = driver.find_element_by_xpath("//input[@tabindex='3']")
        checkOutDate.click()
        # 键盘全选操作,ctrl+A
        checkOutDate.send_keys(Keys.CONTROL, "a")
        checkOutDate.send_keys(Keys.DELETE)
        checkOutDate.send_keys(enddate)

        # 选取开始日期
        checkInDate = driver.find_element_by_xpath("//input[@tabindex='2']")
        checkInDate.click()
        # 键盘全选操作,ctrl+A
        checkInDate.send_keys(Keys.CONTROL, "a")
        checkInDate.send_keys(Keys.DELETE)
        checkInDate.send_keys(startdate)

        # 进行搜索
        search = driver.find_element_by_xpath("//a[@tabindex='5']")
        search.click()
        time.sleep(5)

        # 跳转到最后一页
        search = driver.find_element_by_xpath("//div[@class='list fl_right clearfix']")
        search = search.find_element_by_xpath("//p[@class='item ']=last()")
        search.click()
        time.sleep(5)

        # 页面解析，并创建数据列表
        # 酒店序号
        num = 1
        page_num = 1
        html = driver.page_source
        soup = BeautifulSoup(html, "html.parser")
        totalNum = int(soup.find('div', class_="G_result clearfix patch_MB10").select('div > span')[0].text)
        ing = totalPageNum = math.ceil(totalNum / 20)
        contentList = []
        handle = driver.current_window_handle

        # 定义全局变量
        global new_excel, sheet, row
        try:
            a = int(get_rows() / 20)
            # 跳转到上次跑断的页面
            if a > 0:
                for aa in range(1, a + 1):
                    try:
                        nextPage = driver.find_element_by_xpath("//p[@class='last fl_right not-able']")
                    except:
                        time.sleep(5)
                        nextPage = driver.find_element_by_xpath("//p[@class='last fl_right not-able']")
                    # nextPage.click()
                    try:
                        ActionChains(driver).move_to_element(nextPage).click().perform()
                        print("跳转到第" + str(aa) + "页")
                    except:
                        print("发生异常，重新加载一遍")
                        ActionChains(driver).move_to_element(nextPage).click().perform()
            for l in range(a + 1, totalPageNum):
                print('跑到第 ' + str(l) + ' 页')
                time.sleep(15)
                items = driver.find_elements_by_xpath("//img[@alt='酒店头图']")
                for item in items:
                    # item.click()
                    ActionChains(driver).move_to_element(item).click().perform()
                    # 获取随机数字（10, 15）
                    sleepTime = random.randint(10, 15)
                    time.sleep(sleepTime)
                    handles = driver.window_handles
                    name = ''
                    phone = ''
                    for newHandle in handles:
                        if newHandle != handle:
                            driver.switch_to.window(newHandle)
                            try:
                                name = driver.find_element_by_xpath("//p[@class='name']").text
                                htmlNew = driver.page_source
                                soupNew = BeautifulSoup(htmlNew, "html.parser")
                                phone = soupNew.find('div', class_="hotel_desc").select('div dl dd')[1].text
                                if is_chinese(phone):
                                    phone = soupNew.find('div', class_="hotel_desc").select('div dl dd')[0].text
                                    if is_chinese(phone):
                                        phone = ''
                            except:
                                try:
                                    time.sleep(15)
                                    name = driver.find_element_by_xpath("//p[@class='name']").text
                                    htmlNew = driver.page_source
                                    soupNew = BeautifulSoup(htmlNew, "html.parser")
                                    phone = soupNew.find('div', class_="hotel_desc").select('div dl dd')[1].text
                                    if is_chinese(phone):
                                        phone = soupNew.find('div', class_="hotel_desc").select('div dl dd')[0].text
                                        if is_chinese(phone):
                                            phone = ''
                                except:
                                    print("exception", name, phone)
                            finally:
                                driver.switch_to.window(newHandle)
                                driver.close()
                    driver.switch_to.window(handle)
                    print(name, "——————————", phone)
                    contentSgl = [num, name, phone, startdate, enddate]
                    contentList.append(contentSgl)
                    num += 1
                # 解析多页
                time.sleep(5)
                try:
                    nextPage = driver.find_element_by_xpath("//p[@class='next fl_right cur able']")
                except:
                    time.sleep(15)
                    nextPage = driver.find_element_by_xpath("//p[@class='next fl_right cur able']")
                # nextPage.click()
                try:
                    ActionChains(driver).move_to_element(nextPage).click().perform()
                except:
                    time.sleep(15)
                    ActionChains(driver).move_to_element(nextPage).click().perform()
        finally:
            time.sleep(5)
            page_num += 1

            # 第一步，先判断 excel是否存在，如果存在打开加载，如果不存在则创建
            path = 'D:/HInfos2.xls'
            if os.path.exists(path):
                # 直接使用
                old_excel = xlrd.open_workbook(path, formatting_info=True)
                # 将操作文件对象拷贝，变成可写的workbook对象
                new_excel = copy(old_excel)
                # 获得第一个sheet的对象
                sheet = new_excel.get_sheet(0)
                row = len(sheet.rows) - 1
                row = int(row / 20) * 20
            else:
                # 创建Excel表，并写入数据
                # 创建Excel表
                new_excel = xlwt.Workbook()
                # 创建一个sheet对象，命名为“酒店信息”，cell_overwrite_ok表示是否可以覆盖单元格，是Worksheet实例化的一个参数，默认值是False
                sheet = new_excel.add_sheet('酒店信息', cell_overwrite_ok=False)
                # 标题信息行列表
                rowTitle = ['序号', '酒店名称', '电话号码', '入住如期', '离店日期']
                # 遍历写入标题行信息
                for i in range(0, len(rowTitle)):
                    # 其中的'0'表示行, 'i'表示列，0和i指定了表中的单元格，'rowTitle[i]'是向该单元格写入的内容
                    sheet.write(0, i, rowTitle[i])
                # 初始化一个row
                row = 0
            # 遍历写入数据信息
            # 先遍历一下有多少行
            for k in range(0, len(contentList)):
                # 再把每行的每个单元格遍历一下，j表示列数据
                for j in range(0, len(contentList[k])):
                    # （k + row + 1）表示先去掉标题行，再去掉表格中已存在的数据行，j表示列数据，contentList[k][j] 插入单元格数据
                    sheet.write(k + row + 1, j, contentList[k][j])
            new_excel.save(path)


def sheet_method(work_book, add_sheet=[]):
    """
    获取 sheet 页
    :param work_book:  excel名称
    :param add_sheet: 增加sheet 页
    :return:
    """
    wk = work_book
    # rename default sheet
    ss_sheet = wk.get_sheet_by_name('Sheet')
    ss_sheet.title = add_sheet[0]

    for i in range(1, len(add_sheet)):
        # add new sheet
        wk.create_sheet(add_sheet[i])

    # switch to active sheet
    sheet_num = wk.get_sheet_names()
    last_sheet = len(sheet_num) - 1
    sheet_index = sheet_num.index(sheet_num[last_sheet])
    wk.active = sheet_index


def create_excel(filename="filename.xlsx", recreate=False, add_sheet=[]):
    """
    如果excel 文件存在，判断是否需要重新创建，否则不做任何处理。否则，创建excel 文件
    :param add_sheet: 添加sheet 页
    :param recreate: 是否重复创建，默认为False
    :param filename: 文件名称
    :return:
    """

    # .xls后缀结尾的只能用xlwt模块写，只能写2的16次方即65,535条数据
    # path = os.path.join(os.getcwd()+r'\test.xls')
    # .xlsx的只能用openpyxl模块写，这模块能写2的32次方即4,294,967,296的数据
    path = os.path.join(os.getcwd() + "\\" + filename + ".xlsx")
    p1 = os.path.exists(path)
    if p1:
        if recreate:
            os.remove(path)
            wk = workbook.Workbook()
            sheet_method(wk, add_sheet)
            # save workbook
            wk.save(path)
            print("Had been deleted and recreated " + filename)
        else:
            # error_info = 'Exist ' + filename + ' in path: ' + path\
            #              + " Please use another filename, otherwise the "\
            #              + filename + " file will be override"\
            #              + " if you need override please set recreate as Ture"
            # raise Exception, error_info
            pass
    else:
        # filename = xlwt.Workbook()
        wk = workbook.Workbook()
        sheet_method(wk, add_sheet)
        # save workbook
        wk.save(path)
        # print("you had create " + filename)


def xxx():
    global aaa
    for aa in range(1, 100):
        aaa = aa + aaa
    print(aaa)

if __name__ == '__main__':
    spider = QunarSpider()
    # city = input("请输入城市：")
    # startdate = input("请输入入住日期（yyyy-mm-dd):")
    # enddate = input("请输入离店日期（yyyy-mm-dd):")
    # totalpagenum = input("请输入爬取页数：")
    print("正在爬取中...")
    # spider.theSpider(city, startdate, enddate,totalpagenum)
    #spider.theSpider('北京', '2021-02-27', '2021-02-28')
    # print(random.randint(3, 10))

    # # 直接使用
    # old_excel = xlrd.open_workbook('D:/HInfos.xls', formatting_info=True)
    # # 将操作文件对象拷贝，变成可写的workbook对象
    # new_excel = copy(old_excel)
    # # 获得第一个sheet的对象
    # sheet = new_excel.get_sheet(0)
    # row = len(sheet.rows)
    # a = int(53 / 20) * 20
    # print(a)

    # threads = []
    # for browser, url in data.items():
    #     # 多线程
    #     t1 = threading.Thread(target=test_search, args=(browser, url,))
    #     threads.append(t1)
    # # 启动
    # for t2 in threads:
    #     t2.start()
    #     t2.join()
    aaa = 0
    xxx()